<?php

namespace App\Http\Controllers\App;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Queue;
/**
 * 队列管理控制器
 */
// class QueueController extends Controller
class QueueController extends BaseController
{
    /**
     * 队列列表(GET) api/queue/queuelist
     * 
     * @return json
     */
    public function queueList(Request $request)
    {
        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id

        if(!$shop_id){

            return self::error('未登录');

        }

        // 初始化数组
        $where = $return = array();

        // 构造查询条件
        $where['shop_id']   = $shop_id;

        // 查询列表
        $return = Queue::where($where)->first();

        return self::json($return,'查询成功');
    }

    /**
     * 发布队列(POST) api/queue/addqueue
     * 
     * @return json
     */
    public function addQueue(Request $request)
    {
        // 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id

        if(!$shop_id){

            return self::error('未登录');

        }

        // 是否已发队列
        $ishas = Queue::where('shop_id',$shop_id)->first();

        if($ishas){

            return self::error('已有队列');

        }

        // 数据整理
        $name       = $request->input('name', '');          // 队列名称
        $min_amount = $request->input('min_amount', 0);     // 最低消费
        $max_amount = $request->input('max_amount', 0);     // 最高消费
        $day_amount = $request->input('day_amount', 0);     // 每日最高消费
        $spec       = $request->input('spec', 0);           // 最小间隔人数
        $poundage   = $request->input('poundage', 0);       // 手续费
        $desc       = $request->input('desc', '');          // 描述
        $protocol   = $request->input('protocol', '');      // 协议规则
        $status     = $request->input('status',1);          // 状态 1关闭 2启用
        $reason     = $request->input('close_reason','');   // 关闭原因

        // 必填项判断
        if(empty($name) || $min_amount<=0 || $max_amount<=0 || $day_amount<=0){

            return self::error('必填项信息不完整');

        }

        // 实例化model
        $queue = new Queue;

        $queue->name        = $name;
        $queue->shop_id     = $shop_id;
        $queue->min_amount  = $min_amount;
        $queue->max_amount  = $max_amount;
        $queue->day_amount  = $day_amount;
        $queue->spec        = $spec;
        $queue->poundage    = $poundage;
        $queue->desc        = $desc;
        $queue->protocol    = $protocol;
        $queue->status      = $status;
        $queue->close_reason= $reason;

        // 数据入表
        $re = $queue->save();
        if($re){

            return self::success('添加成功');

        }else{

            return self::error('添加失败');

        }   
    }

    /**
     * 队列编辑(GET) api/queue/upqueue
     *
     * @return json
     */
    public function upQueue(Request $request)
    {
    	// 商户SESSION数据
        $shopInfo = $request->session()->get('shopInfo');

        // 商户状态判断
        $shop_id = $shopInfo['id'];     // 店铺id

        if(!$shop_id){

            return self::error('未登录');

        }

        $id = $request->input('id');

        if(!$id){

            return self::error('缺少参数');

        }

        // 初始化数组
        $data = array();

        // 数据整理
        $request->input('name')         == null || $data['name']        = $request->input('name');              // 商品
        $request->input('min_amount')   == null || $data['min_amount']  = $request->input('min_amount');        // 最小消费额度
        $request->input('max_amount')   == null || $data['max_amount']  = $request->input('max_amount');        // 最高消费额度
        $request->input('day_amount')   == null || $data['day_amount']  = $request->input('day_amount');        // 当日最高消费
        $request->input('spec')         == null || $data['spec']        = $request->input('spec');              // 最小间隔人数
        $request->input('poundage')     == null || $data['poundage']    = $request->input('poundage');          // 手续费
        $request->input('desc')         == null || $data['desc']        = $request->input('desc');              // 描述
        $request->input('protocol')     == null || $data['protocol']    = $request->input('protocol');          // 协议规则
        $request->input('status')       == null || $data['status']      = $request->input('status');            // 状态 2关闭 1启用
        $request->input('close_reason') == null || $data['close_reason']= $request->input('close_reason');      // 关闭原因

        $re = Queue::where('id',$id)->where('shop_id',$shop_id)->update($data);

        if($re){

            return self::success('更新成功');

        }else{

            return self::error('更新失败');
            
        }   
    }

    

}
